//*****************************************************************************
//
//								Monitor_CPU.c
//
//*****************************************************************************
//
//
#include "Config.h"
#include "reg.h"
#include "typedefs.h"
#include "reg_debug.h"
#include "printf.h"
#include "CPU.h"
#include "util.h"
#include "Grobal.h"

//=============================================================================
BYTE WriteSFR(BYTE addr, BYTE dat)
{
	switch (addr) {
		case 0x80:	SFR_80 = dat;	if( echo ) return SFR_80; else return 0;
		case 0x81:	SFR_81 = dat;	if( echo ) return SFR_81; else return 0;
		case 0x82:	SFR_82 = dat;	if( echo ) return SFR_82; else return 0;
		case 0x83:	SFR_83 = dat;	if( echo ) return SFR_83; else return 0;
		case 0x84:	SFR_84 = dat;	if( echo ) return SFR_84; else return 0;
		case 0x85:	SFR_85 = dat;	if( echo ) return SFR_85; else return 0;
		case 0x86:	SFR_86 = dat;	if( echo ) return SFR_86; else return 0;
		case 0x87:	SFR_87 = dat;	if( echo ) return SFR_87; else return 0;
		case 0x88:	SFR_88 = dat;	if( echo ) return SFR_88; else return 0;
		case 0x89:	SFR_89 = dat;	if( echo ) return SFR_89; else return 0;
		case 0x8a:	SFR_8a = dat;	if( echo ) return SFR_8a; else return 0;
		case 0x8b:	SFR_8b = dat;	if( echo ) return SFR_8b; else return 0;
		case 0x8c:	SFR_8c = dat;	if( echo ) return SFR_8c; else return 0;
		case 0x8d:	SFR_8d = dat;	if( echo ) return SFR_8d; else return 0;
		case 0x8e:	SFR_8e = dat;	if( echo ) return SFR_8e; else return 0;
		case 0x8f:	SFR_8f = dat;	if( echo ) return SFR_8f; else return 0;

		case 0x90:	SFR_90 = dat;	if( echo ) return SFR_90; else return 0;
		case 0x91:	SFR_91 = dat;	if( echo ) return SFR_91; else return 0;
		case 0x92:	SFR_92 = dat;	if( echo ) return SFR_92; else return 0;
		case 0x93:	SFR_93 = dat;	if( echo ) return SFR_93; else return 0;
		case 0x94:	SFR_94 = dat;	if( echo ) return SFR_94; else return 0;
		case 0x95:	SFR_95 = dat;	if( echo ) return SFR_95; else return 0;
		case 0x96:	SFR_96 = dat;	if( echo ) return SFR_96; else return 0;
		case 0x97:	SFR_97 = dat;	if( echo ) return SFR_97; else return 0;
		case 0x98:	SFR_98 = dat;	if( echo ) return SFR_98; else return 0;
		case 0x99:	SFR_99 = dat;	if( echo ) return SFR_99; else return 0;
		case 0x9a:	SFR_9a = dat;	if( echo ) return SFR_9a; else return 0;
		case 0x9b:	SFR_9b = dat;	if( echo ) return SFR_9b; else return 0;
		case 0x9c:	SFR_9c = dat;	if( echo ) return SFR_9c; else return 0;
		case 0x9d:	SFR_9d = dat;	if( echo ) return SFR_9d; else return 0;
		case 0x9e:	SFR_9e = dat;	if( echo ) return SFR_9e; else return 0;
		case 0x9f:	SFR_9f = dat;	if( echo ) return SFR_9f; else return 0;
                                                  
		case 0xa0:	SFR_a0 = dat;	if( echo ) return SFR_a0; else return 0;
		case 0xa1:	SFR_a1 = dat;	if( echo ) return SFR_a1; else return 0;
		case 0xa2:	SFR_a2 = dat;	if( echo ) return SFR_a2; else return 0;
		case 0xa3:	SFR_a3 = dat;	if( echo ) return SFR_a3; else return 0;
		case 0xa4:	SFR_a4 = dat;	if( echo ) return SFR_a4; else return 0;
		case 0xa5:	SFR_a5 = dat;	if( echo ) return SFR_a5; else return 0;
		case 0xa6:	SFR_a6 = dat;	if( echo ) return SFR_a6; else return 0;
		case 0xa7:	SFR_a7 = dat;	if( echo ) return SFR_a7; else return 0;
		case 0xa8:	SFR_a8 = dat;	if( echo ) return SFR_a8; else return 0;
		case 0xa9:	SFR_a9 = dat;	if( echo ) return SFR_a9; else return 0;
		case 0xaa:	SFR_aa = dat;	if( echo ) return SFR_aa; else return 0;
		case 0xab:	SFR_ab = dat;	if( echo ) return SFR_ab; else return 0;
		case 0xac:	SFR_ac = dat;	if( echo ) return SFR_ac; else return 0;
		case 0xad:	SFR_ad = dat;	if( echo ) return SFR_ad; else return 0;
		case 0xae:	SFR_ae = dat;	if( echo ) return SFR_ae; else return 0;
		case 0xaf:	SFR_af = dat;	if( echo ) return SFR_af; else return 0;
                                                  
		case 0xb0:	SFR_b0 = dat;	if( echo ) return SFR_b0; else return 0;
		case 0xb1:	SFR_b1 = dat;	if( echo ) return SFR_b1; else return 0;
		case 0xb2:	SFR_b2 = dat;	if( echo ) return SFR_b2; else return 0;
		case 0xb3:	SFR_b3 = dat;	if( echo ) return SFR_b3; else return 0;
		case 0xb4:	SFR_b4 = dat;	if( echo ) return SFR_b4; else return 0;
		case 0xb5:	SFR_b5 = dat;	if( echo ) return SFR_b5; else return 0;
		case 0xb6:	SFR_b6 = dat;	if( echo ) return SFR_b6; else return 0;
		case 0xb7:	SFR_b7 = dat;	if( echo ) return SFR_b7; else return 0;
		case 0xb8:	SFR_b8 = dat;	if( echo ) return SFR_b8; else return 0;
		case 0xb9:	SFR_b9 = dat;	if( echo ) return SFR_b9; else return 0;
		case 0xba:	SFR_ba = dat;	if( echo ) return SFR_ba; else return 0;
		case 0xbb:	SFR_bb = dat;	if( echo ) return SFR_bb; else return 0;
		case 0xbc:	SFR_bc = dat;	if( echo ) return SFR_bc; else return 0;
		case 0xbd:	SFR_bd = dat;	if( echo ) return SFR_bd; else return 0;
		case 0xbe:	SFR_be = dat;	if( echo ) return SFR_be; else return 0;
		case 0xbf:	SFR_bf = dat;	if( echo ) return SFR_bf; else return 0;
                                                  
		case 0xc0:	SFR_c0 = dat;	if( echo ) return SFR_c0; else return 0;
		case 0xc1:	SFR_c1 = dat;	if( echo ) return SFR_c1; else return 0;
		case 0xc2:	SFR_c2 = dat;	if( echo ) return SFR_c2; else return 0;
		case 0xc3:	SFR_c3 = dat;	if( echo ) return SFR_c3; else return 0;
		case 0xc4:	SFR_c4 = dat;	if( echo ) return SFR_c4; else return 0;
		case 0xc5:	SFR_c5 = dat;	if( echo ) return SFR_c5; else return 0;
		case 0xc6:	SFR_c6 = dat;	if( echo ) return SFR_c6; else return 0;
		case 0xc7:	SFR_c7 = dat;	if( echo ) return SFR_c7; else return 0;
		case 0xc8:	SFR_c8 = dat;	if( echo ) return SFR_c8; else return 0;
		case 0xc9:	SFR_c9 = dat;	if( echo ) return SFR_c9; else return 0;
		case 0xca:	SFR_ca = dat;	if( echo ) return SFR_ca; else return 0;
		case 0xcb:	SFR_cb = dat;	if( echo ) return SFR_cb; else return 0;
		case 0xcc:	SFR_cc = dat;	if( echo ) return SFR_cc; else return 0;
		case 0xcd:	SFR_cd = dat;	if( echo ) return SFR_cd; else return 0;
		case 0xce:	SFR_ce = dat;	if( echo ) return SFR_ce; else return 0;
		case 0xcf:	SFR_cf = dat;	if( echo ) return SFR_cf; else return 0;
                                                  
		case 0xd0:	SFR_d0 = dat;	if( echo ) return SFR_d0; else return 0;
		case 0xd1:	SFR_d1 = dat;	if( echo ) return SFR_d1; else return 0;
		case 0xd2:	SFR_d2 = dat;	if( echo ) return SFR_d2; else return 0;
		case 0xd3:	SFR_d3 = dat;	if( echo ) return SFR_d3; else return 0;
		case 0xd4:	SFR_d4 = dat;	if( echo ) return SFR_d4; else return 0;
		case 0xd5:	SFR_d5 = dat;	if( echo ) return SFR_d5; else return 0;
		case 0xd6:	SFR_d6 = dat;	if( echo ) return SFR_d6; else return 0;
		case 0xd7:	SFR_d7 = dat;	if( echo ) return SFR_d7; else return 0;
		case 0xd8:	SFR_eb = 0xaa; 
					SFR_eb = 0x55; 
					SFR_d8 = dat;	if( echo ) return SFR_d8; else return 0;
		case 0xd9:	SFR_d9 = dat;	if( echo ) return SFR_d9; else return 0;
		case 0xda:	SFR_da = dat;	if( echo ) return SFR_da; else return 0;
		case 0xdb:	SFR_db = dat;	if( echo ) return SFR_db; else return 0;
		case 0xdc:	SFR_dc = dat;	if( echo ) return SFR_dc; else return 0;
		case 0xdd:	SFR_dd = dat;	if( echo ) return SFR_dd; else return 0;
		case 0xde:	SFR_de = dat;	if( echo ) return SFR_de; else return 0;
		case 0xdf:	SFR_df = dat;	if( echo ) return SFR_df; else return 0;
                                                  
		case 0xe0:	SFR_e0 = dat;	if( echo ) return SFR_e0; else return 0;
		case 0xe1:	SFR_e1 = dat;	if( echo ) return SFR_e1; else return 0;
		case 0xe2:	SFR_e2 = dat;	if( echo ) return SFR_e2; else return 0;
		case 0xe3:	SFR_e3 = dat;	if( echo ) return SFR_e3; else return 0;
		case 0xe4:	SFR_e4 = dat;	if( echo ) return SFR_e4; else return 0;
		case 0xe5:	SFR_e5 = dat;	if( echo ) return SFR_e5; else return 0;
		case 0xe6:	SFR_e6 = dat;	if( echo ) return SFR_e6; else return 0;
		case 0xe7:	SFR_e7 = dat;	if( echo ) return SFR_e7; else return 0;
		case 0xe8:	SFR_e8 = dat;	if( echo ) return SFR_e8; else return 0;
		case 0xe9:	SFR_e9 = dat;	if( echo ) return SFR_e9; else return 0;
		case 0xea:	SFR_ea = dat;	if( echo ) return SFR_ea; else return 0;
		case 0xeb:	SFR_eb = dat;	if( echo ) return SFR_eb; else return 0;
		case 0xec:	SFR_ec = dat;	if( echo ) return SFR_ec; else return 0;
		case 0xed:	SFR_ed = dat;	if( echo ) return SFR_ed; else return 0;
		case 0xee:	SFR_ee = dat;	if( echo ) return SFR_ee; else return 0;
		case 0xef:	SFR_ef = dat;	if( echo ) return SFR_ef; else return 0;
                                                  
		case 0xf0:	SFR_f0 = dat;	if( echo ) return SFR_f0; else return 0;
		case 0xf1:	SFR_f1 = dat;	if( echo ) return SFR_f1; else return 0;
		case 0xf2:	SFR_f2 = dat;	if( echo ) return SFR_f2; else return 0;
		case 0xf3:	SFR_f3 = dat;	if( echo ) return SFR_f3; else return 0;
		case 0xf4:	SFR_f4 = dat;	if( echo ) return SFR_f4; else return 0;
		case 0xf5:	SFR_f5 = dat;	if( echo ) return SFR_f5; else return 0;
		case 0xf6:	SFR_f6 = dat;	if( echo ) return SFR_f6; else return 0;
		case 0xf7:	SFR_f7 = dat;	if( echo ) return SFR_f7; else return 0;
		case 0xf8:	SFR_f8 = dat;	if( echo ) return SFR_f8; else return 0;
		case 0xf9:	SFR_f9 = dat;	if( echo ) return SFR_f9; else return 0;
		case 0xfa:	SFR_fa = dat;	if( echo ) return SFR_fa; else return 0;
		case 0xfb:	SFR_fb = dat;	if( echo ) return SFR_fb; else return 0;
		case 0xfc:	SFR_fc = dat;	if( echo ) return SFR_fc; else return 0;
		case 0xfd:	SFR_fd = dat;	if( echo ) return SFR_fd; else return 0;
		case 0xfe:	SFR_fe = dat;	if( echo ) return SFR_fe; else return 0;
		case 0xff:	SFR_ff = dat;	if( echo ) return SFR_ff; else return 0;
	}
	return 0;
}

//=============================================================================
BYTE ReadSFR(BYTE addr)
{
	switch (addr) {
		case 0x80:	return SFR_80;
		case 0x81:	return SFR_81;
		case 0x82:	return SFR_82;
		case 0x83:	return SFR_83;
		case 0x84:	return SFR_84;
		case 0x85:	return SFR_85;
		case 0x86:	return SFR_86;
		case 0x87:	return SFR_87;
		case 0x88:	return SFR_88;
		case 0x89:	return SFR_89;
		case 0x8a:	return SFR_8a;
		case 0x8b:	return SFR_8b;
		case 0x8c:	return SFR_8c;
		case 0x8d:	return SFR_8d;
		case 0x8e:	return SFR_8e;
		case 0x8f:	return SFR_8f;
                                  
		case 0x90:	return SFR_90;
		case 0x91:	return SFR_91;
		case 0x92:	return SFR_92;
		case 0x93:	return SFR_93;
		case 0x94:	return SFR_94;
		case 0x95:	return SFR_95;
		case 0x96:	return SFR_96;
		case 0x97:	return SFR_97;
		case 0x98:	return SFR_98;
		case 0x99:	return SFR_99;
		case 0x9a:	return SFR_9a;
		case 0x9b:	return SFR_9b;
		case 0x9c:	return SFR_9c;
		case 0x9d:	return SFR_9d;
		case 0x9e:	return SFR_9e;
		case 0x9f:	return SFR_9f;
                                  
		case 0xa0:	return SFR_a0;
		case 0xa1:	return SFR_a1;
		case 0xa2:	return SFR_a2;
		case 0xa3:	return SFR_a3;
		case 0xa4:	return SFR_a4;
		case 0xa5:	return SFR_a5;
		case 0xa6:	return SFR_a6;
		case 0xa7:	return SFR_a7;
		case 0xa8:	return SFR_a8;
		case 0xa9:	return SFR_a9;
		case 0xaa:	return SFR_aa;
		case 0xab:	return SFR_ab;
		case 0xac:	return SFR_ac;
		case 0xad:	return SFR_ad;
		case 0xae:	return SFR_ae;
		case 0xaf:	return SFR_af;
                                  
		case 0xb0:	return SFR_b0;
		case 0xb1:	return SFR_b1;
		case 0xb2:	return SFR_b2;
		case 0xb3:	return SFR_b3;
		case 0xb4:	return SFR_b4;
		case 0xb5:	return SFR_b5;
		case 0xb6:	return SFR_b6;
		case 0xb7:	return SFR_b7;
		case 0xb8:	return SFR_b8;
		case 0xb9:	return SFR_b9;
		case 0xba:	return SFR_ba;
		case 0xbb:	return SFR_bb;
		case 0xbc:	return SFR_bc;
		case 0xbd:	return SFR_bd;
		case 0xbe:	return SFR_be;
		case 0xbf:	return SFR_bf;
                                  
		case 0xc0:	return SFR_c0;
		case 0xc1:	return SFR_c1;
		case 0xc2:	return SFR_c2;
		case 0xc3:	return SFR_c3;
		case 0xc4:	return SFR_c4;
		case 0xc5:	return SFR_c5;
		case 0xc6:	return SFR_c6;
		case 0xc7:	return SFR_c7;
		case 0xc8:	return SFR_c8;
		case 0xc9:	return SFR_c9;
		case 0xca:	return SFR_ca;
		case 0xcb:	return SFR_cb;
		case 0xcc:	return SFR_cc;
		case 0xcd:	return SFR_cd;
		case 0xce:	return SFR_ce;
		case 0xcf:	return SFR_cf;
                                  
		case 0xd0:	return SFR_d0;
		case 0xd1:	return SFR_d1;
		case 0xd2:	return SFR_d2;
		case 0xd3:	return SFR_d3;
		case 0xd4:	return SFR_d4;
		case 0xd5:	return SFR_d5;
		case 0xd6:	return SFR_d6;
		case 0xd7:	return SFR_d7;
		case 0xd8:	return SFR_d8;
		case 0xd9:	return SFR_d9;
		case 0xda:	return SFR_da;
		case 0xdb:	return SFR_db;
		case 0xdc:	return SFR_dc;
		case 0xdd:	return SFR_dd;
		case 0xde:	return SFR_de;
		case 0xdf:	return SFR_df;
                                  
		case 0xe0:	return SFR_e0;
		case 0xe1:	return SFR_e1;
		case 0xe2:	return SFR_e2;
		case 0xe3:	return SFR_e3;
		case 0xe4:	return SFR_e4;
		case 0xe5:	return SFR_e5;
		case 0xe6:	return SFR_e6;
		case 0xe7:	return SFR_e7;
		case 0xe8:	return SFR_e8;
		case 0xe9:	return SFR_e9;
		case 0xea:	return SFR_ea;
		case 0xeb:	return SFR_eb;
		case 0xec:	return SFR_ec;
		case 0xed:	return SFR_ed;
		case 0xee:	return SFR_ee;
		case 0xef:	return SFR_ef;
                                  
		case 0xf0:	return SFR_f0;
		case 0xf1:	return SFR_f1;
		case 0xf2:	return SFR_f2;
		case 0xf3:	return SFR_f3;
		case 0xf4:	return SFR_f4;
		case 0xf5:	return SFR_f5;
		case 0xf6:	return SFR_f6;
		case 0xf7:	return SFR_f7;
		case 0xf8:	return SFR_f8;
		case 0xf9:	return SFR_f9;
		case 0xfa:	return SFR_fa;
		case 0xfb:	return SFR_fb;
		case 0xfc:	return SFR_fc;
		case 0xfd:	return SFR_fd;
		case 0xfe:	return SFR_fe;
		case 0xff:	return SFR_ff;

	}
}
//=============================================================================
//
//=============================================================================
void MonitorCPU(void)
{
	BYTE ret, i, j;

	BYTE argv_num1 = a2h( argv[1] );
	BYTE argv_num2 = a2h( argv[2] );
	BYTE argv_num3 = a2h( argv[3] );

	//---------------------- Write SFR -----------------------
	if( !stricmp( argv[0], "MW" ) ) {
		if( argc < 3 ) {
			Printf(" ----> Missing parameter !!!");
			return;
		}
		else ret = WriteSFR(argv_num1, argv_num2);
		if( echo ) {
			Printf("\nMCU SFR Write: %02bx=%02bx (%02bx) ", argv_num1, argv_num2, ret);
			for(i=0; i<8; i++) {
				if( ret & 0x80 ) Puts("1"); else Puts("0");
				ret <<=1;
			}
		}
	}

	//---------------------- Read SFR ------------------------
	else if( !stricmp( argv[0], "MR" ) ) {
		if( argc < 2 ) {
			Printf(" ----> Missing parameter !!!");
			return;
		}
		else ret = ReadSFR(argv_num1);
		if( echo ) {
			Printf("\nMCU SFR Read: %02bx=%02bx ", argv_num1, ret);
			for(i=0; i<8; i++) {
				if( ret & 0x80 ) Puts("1"); else Puts("0");
				ret <<=1;
			}
		}
	}

	//---------------------- Dump SFR ------------------------
	else if( !stricmp( argv[0], "MD" ) ) {

		Printf("\nDump DP8051 SFR");
		for (j=0x8; j<=0x0f; j++) {
			Printf("\nSFR %02bx: ", j*0x10);
			for(i=0; i<8; i++) Printf("%02bx ", ReadSFR(j*0x10+i) );
			Printf("- ");
			for(; i<16; i++) Printf("%02bx ", ReadSFR(j*0x10+i) );
		}

	}
	//---------------------- Enable WDT ------------------------
	else if( !stricmp( argv[0], "MWDT" ) ) {

		if( argv[1][0]=='1' ) {
			EnableWatchdog();
			Printf("\nWatchdog Timer is enabled.");
		}
		else {
			DisableWatchdog();
			Printf("\nWatchdog Timer is disabled.");
		}

	}
	//--------------------------------------------------------
	else
		Printf("\nInvalid command...");	
	
}


